package javaer.starter.model.context;

import javaer.starter.placeholder.ParameterMapping;
import javaer.starter.idea.window.component.ProgressComponent;
import javaer.starter.model.DependencyExcludedModel;
import javaer.starter.model.DependencyModel;
import javaer.starter.model.ModuleDependencyModel;
import javaer.starter.model.ModuleModel;
import javaer.starter.model.StarterTypeModel;
import javaer.starter.model.TemplateModel;
import lombok.AccessLevel;
import lombok.experimental.FieldDefaults;

import java.io.Serializable;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @author yangzhen
 * @date 2023/7/5 10:17
 * @since 1.0.0
 */
@FieldDefaults(level = AccessLevel.PRIVATE)
public class StarterContext implements Serializable {

    // 模板化的文件数
    public int fileCount = 0;
    // 已处理的文件数，用于进度条展示
    public volatile int dealCount = 0;
    // 项目的根artifactId
    public String rootArtifactId;
    // 基础包名
    public String basePackage;
    // k：模块名称 v：artifactId
    public Map<String, String> moduleId_artifactId = new HashMap<>();
    // k：artifactId v：模块名称
    public Map<String, String> artifactId_moduleId = new HashMap<>();
    // k：artifactId v：pom的path
    public Map<String, Path> artifactId_modulePath = new HashMap<>();
    // k：artifactId v：模块依赖
//    public Map<String, Set<ModuleDependencyModel>> artifactId_moduleDependency = new HashMap<>();

    public Set<ModuleDependencyModel> moduleDependencyModels = new HashSet<>();

    // 脚手架类型
    public StarterTypeModel starterTypeModel;
    // 模块
    public Set<ModuleModel> moduleModels = new HashSet<>();
    // 依赖
    public Set<DependencyModel> dependencies = new HashSet<>();
    // 依赖排除
    public Set<DependencyExcludedModel> excludedDependencies = new HashSet<>();
    // 模板
    public Set<TemplateModel> templateModels = new HashSet<>();
    // 类的全限定名
    public Set<String> qualifiedNames = new HashSet<>();
    // 环境变量映射
    public Set<ParameterMapping> parameterMappings = new HashSet<>();

    public ProgressComponent progressComponent;

    public void addAllParameter(Set<ParameterMapping> parameterMappings) {
        if (parameterMappings == null || parameterMappings.isEmpty()) {
            return;
        }
        this.parameterMappings.addAll(parameterMappings);
    }

    public void addArtifactIdModulePath(String artifactId, Path path) {
        artifactId_modulePath.put(artifactId, path);
    }

    public void addQualifiedNames(Set<String> qualifiedName) {
        this.qualifiedNames.addAll(qualifiedName);
    }

    public void addTemplates(Set<TemplateModel> templateModels) {
        fileCount = fileCount + templateModels.size();
        this.templateModels.addAll(templateModels);
    }

    public void addModuleDependencies(Set<ModuleDependencyModel> moduleDependencyModels) {
        this.moduleDependencyModels.addAll(moduleDependencyModels);
    }

    public void addDependencies(Set<DependencyModel> dependencies) {
        this.dependencies.addAll(dependencies);
    }

    public void addExcludedDependencies(Set<DependencyExcludedModel> excludedDependencies) {
        this.excludedDependencies.addAll(excludedDependencies);
    }

    public void addModuleModel(ModuleModel moduleModel) {
        moduleModels.add(moduleModel);
    }

    public void addModuleIdArtifactId(String moduleId, String artifactId) {
        moduleId_artifactId.put(moduleId, artifactId);
        artifactId_moduleId.put(artifactId, moduleId);
    }

    public String getBasePackage() {
        return basePackage;
    }

    public void setBasePackage(String basePackage) {
        this.basePackage = basePackage;
    }

    public StarterTypeModel getStarterTypeModel() {
        return starterTypeModel;
    }

    public void setStarterTypeModel(StarterTypeModel starterTypeModel) {
        this.starterTypeModel = starterTypeModel;
    }

    public Set<ModuleModel> getModuleModels() {
        return moduleModels;
    }

    public void setModuleModels(Set<ModuleModel> moduleModels) {
        this.moduleModels = moduleModels;
    }

    public Set<DependencyModel> getDependencies() {
        return dependencies;
    }

    public void setDependencies(Set<DependencyModel> dependencies) {
        this.dependencies = dependencies;
    }

    public String getRootArtifactId() {
        return rootArtifactId;
    }

    public void setRootArtifactId(String rootArtifactId) {
        this.rootArtifactId = rootArtifactId;
    }

    public String getTypeId() {
        return this.starterTypeModel.getTypeId();
    }

    public Set<DependencyExcludedModel> getExcludedDependencies() {
        return excludedDependencies;
    }

    public void setExcludedDependencies(Set<DependencyExcludedModel> excludedDependencies) {
        this.excludedDependencies = excludedDependencies;
    }

    public Set<TemplateModel> getTemplateModels() {
        return templateModels;
    }

    public Set<ModuleDependencyModel> getModuleDependencyModels() {
        return moduleDependencyModels;
    }

    //    public Map<String, Set<ModuleDependencyModel>> getArtifactId_moduleDependency() {
//        return artifactId_moduleDependency;
//    }
}
